//
//  ANUserInfoTableViewController.m
//  Ants
//
//  Created by ZHM on 16/4/25.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANUserInfoTableViewController.h"
#import "ANProfieleHeader.h"
#import "ANTableViewProfileCell.h"
#import "ANEditorInfoViewController.h"
#import "ANOptionsView.h"

#define ANlogoutComplete @"logoutComplete"

@interface ANUserInfoTableViewController ()<ANEditorInfoViewControllerDelegater,ANOptionsViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, weak) ANTableViewProfileCell  *currentCell;
@property (nonatomic, weak) ANIconItem *userIcon;

@end

@implementation ANUserInfoTableViewController

- (instancetype)init {
    if (self = [super init]){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人信息";
    //设置导航条
    //[self setUpNav];
    
    //添加第1组
    [self setUpGroup_1];
    
    //添加第1组
    [self setUpGroup_2];
}

- (void)setUpGroup_1 {
    ANBmobUser *user = [sharedWebService currentUser];
    ANLog(@"%p=>",user);
    ANIconItem *userIcon = [ANIconItem itemWithTilte: @"头像" subTilte:nil];
    userIcon.iconPath = user.iconImage;
    ANLog(@"VC:user = %@",user.iconImage);
    __weak typeof(self) weakSelf = self;
    userIcon.option = ^(ANProfileBaseItem *item){
        ANLog(@"调用这里了!!!");
        [weakSelf chooseImage];
    };
    _userIcon = userIcon;
    
    ANProfileBaseItem *userName = [ANProfileBaseItem itemWithTilte: @"用户名" subTilte:user.username];
    ANArrowItem *nickName = [ANArrowItem itemWithTilte: @"昵称" subTilte:user.nickName];
    nickName.destVcClass = [ANEditorInfoViewController class];
    ANArrowItem *tel = [ANArrowItem itemWithTilte: @"手机" subTilte:user.tel];
    tel.destVcClass = [ANEditorInfoViewController class];

    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    group.items = @[userIcon,userName,nickName,tel];
    [self.guoups addObject:group];
}
#pragma mark --选择图片
- (void)chooseImage {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"照相" otherButtonTitles:@"相册", nil];
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    ANLog(@"%ld",(long)buttonIndex);
    
    //图片选择器
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    imagePicker.delegate = self;
    
    //允许编辑图片
    imagePicker.allowsEditing = YES;
    
    switch (buttonIndex) {
        case 0:
            //照相
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            break;
        case 1:
            //相册
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            break;
        case 2:
            return;
            break;
    }
    //显示控制器
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark-图片选择完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    ANLog(@"%@",info);
    UIImage *editedimage = info[UIImagePickerControllerEditedImage];
    
    [sharedWebService updatePhoto:editedimage completion:^(NSString *filePath, BOOL succeeded, NSError *error) {
        if (succeeded) {
            ANLog(@"上传成功 地址:%@",filePath);
            _userIcon.iconPath = filePath;
            [self.tableView reloadData];
        } else {
            
        }
    }];
    //移除图片选择器
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)setUpGroup_2 {
    __weak typeof(self) weakSelf = self;
    ANlableItem *exit = [[ANlableItem alloc] init];
    exit.text = @"退出当前账号";
    exit.option = ^(ANProfileBaseItem *item){
        ANlableItem *exitItem = (ANlableItem *)item;
        //显示选择视图
        [ANOptionsView optionsViewWithTitle:exitItem.text delegate:weakSelf];
    };
    ANGuoupItem *group = [[ANGuoupItem alloc] init];
    group.items = @[exit];
    [self.guoups addObject:group];
}

//代理方法
- (void)optionsView:(ANOptionsView *)optionsView didClickBtn:(NSInteger)index {
    if (index == 1) {
        //退出当前账号
        [sharedWebService logout];
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:ANlogoutComplete object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshShoppingCar" object:nil];
        return;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ANTableViewProfileCell *cell = [ANTableViewProfileCell cellWithTableView:tableView];
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    cell.item = item;
    return cell;
}
#pragma mark -点击CELL调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexCellClick = indexPath;
    ANTableViewProfileCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ANGuoupItem *group = self.guoups[indexPath.section];
    ANProfileBaseItem *item = group.items[indexPath.row];
    if (item.option) {
        item.option(item);
    }else if (item.destVcClass) {
        ANEditorInfoViewController *vc = [[ANEditorInfoViewController alloc] init];
        vc.item = cell.item;
        self.currentCell = cell;
        vc.delegater = self;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark -- 返回每行CELL高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 0) {
        return 80;
    }
    return 44;
}

#pragma mark - 编辑控制器代理方法
- (void)editControllerdidFinishSave:(ANEditorInfoViewController *)editController {
    //更新CeLL
    self.currentCell.item = editController.item;
}
- (void)dealloc {
    ANLog(@"管理界面消失");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
